package com.pvdnc.xdynamic;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class DaemonService extends Service {
    private static final String TAG=DaemonService.class.getSimpleName();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public static void start(Context context){
        Intent intent=new Intent(context,DaemonService.class);
        context.startService(intent);
    }

    public DaemonService() {
        super();
        Log.d(TAG,"construct new instance:"+this);
        mStr="i";
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG,"onCreate");
    }

    private void log(String content){
        Log.d(TAG,"[DaemonService] "+mStr+" "+content);
    }

    private String mStr;

    public static boolean SERVICE_REPLACED;

    @Override
    public final int onStartCommand(Intent intent, int flags, int startId) {
        try {
            if(intent==null){
                log("intent cannot be null");
                Log.e(TAG,"intent cannot be null");
                return START_STICKY;
            }
            if(SERVICE_REPLACED)
                return super.onStartCommand(intent, flags, startId);
            log("DaemonService");
            SERVICE_REPLACED=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return super.onStartCommand(intent, flags, startId);
    }
}
